﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;

namespace RBAC.Administry
{
    /// <summary>
    /// 页面工具
    /// </summary>
    public static class PageHelper
    {
        /// <summary>
        /// 生成弹出消息
        /// </summary>
        /// <param name="msg">消息文字</param>
        /// <returns>脚本</returns>
        private static string CreateAlert(string msg)
        {
            return string.Format("alert('{0}');", msg);
        }
        /// <summary>
        /// 生成提示脚本
        /// </summary>
        /// <param name="tip">提示文字</param>
        /// <returns>脚本</returns>
        private static string CreateTips(string tip)
        {
            return string.Format("$.jGrowl('{0}', { life: 2000 });", tip);
        }
        /// <summary>
        /// 向页面输出脚本
        /// </summary>
        /// <param name="panel">UpdatePanel实例</param>
        /// <param name="page">页面实例</param>
        /// <param name="script">脚本</param>
        public static void Script(Page page, string script, UpdatePanel panel)
        {
            ScriptManager.RegisterStartupScript(panel, page.GetType(), "call", script, true);
        }
        /// <summary>
        /// 向页面输出脚本
        /// </summary>
        /// <param name="page">页面实例</param>
        /// <param name="script">脚本</param>
        public static void Script(Page page, string script)
        {
            page.ClientScript.RegisterStartupScript(page.GetType(), Guid.NewGuid().ToString(), script);
        }
        /// <summary>
        /// 输出弹出错误脚本
        /// </summary>
        /// <param name="page">页面实例</param>
        /// <param name="msg">错误消息</param>
        public static void Error(Page page, string msg)
        {
            Script(page, CreateAlert(msg));
        }
        /// <summary>
        /// 输出弹出错误脚本
        /// </summary>
        /// <param name="panel">UpdatePanel实例</param>
        /// <param name="page">页面实例</param>
        /// <param name="msg">错误消息</param>
        public static void Error(Page page, string msg, UpdatePanel panel)
        {
            Script(page, CreateAlert(msg), panel);
        }
        /// <summary>
        /// 输出反馈提示
        /// </summary>
        /// <param name="panel">UpdatePanel实例</param>
        /// <param name="page">页面实例</param>
        /// <param name="tips">提示内容</param>
        public static void Tips(Page page, string tips, UpdatePanel panel)
        {
            Script(page, CreateTips(tips), panel);
        }
        /// <summary>
        /// 输出反馈提示
        /// </summary>
        /// <param name="page">页面实例</param>
        /// <param name="tips">提示内容</param>
        public static void Tips(Page page, string tips)
        {
            Script(page, CreateTips(tips));
        }
    }

}
